<?php

class system extends BACKEND_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */

    function __construct() {
        parent::__construct();
        $this->load->model('backend/setting_model');
        $this->smarty = new CI_Smarty3();
        $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
    }

    function index() {
        
    }
    function lang($lang='en'){
        $this->load->model('backend/lang_model');
        $colModel = $this->lang_model->colmod();
        foreach ($colModel as $col) {
            $datafields[] = array(
                'name' => $col['datafield'],
                'type' => isset($col['type']) ? $col['type'] : 'string'
            );
            $column = $col;
            $column['editable'] = isset($col['editable']) ? $col['editable'] : false;
            $column['filterable'] = isset($col['filterable']) ? $col['filterable'] : true;
            $column['sortable'] = isset($col['sortable']) ? $col['sortable'] : true;
            $column['minwidth'] = isset($col['minwidth']) ? $col['minwidth'] : (isset($col['width']) ? $col['width'] : 100);
            $columns[$col['datafield']] = $column;
            $list[] = array(
                'label' => $col['text'],
                'value' => $col['datafield'],
                'checked' => isset($col["hidden"]) ? !$col["hidden"] : true
            );
        }
        $assigns['datafields']= $datafields;
        $assigns['columns']= $columns;
        $assigns['list']= $list;
        $assigns['lang']=$lang;
        $lang=isset($_GET['lang'])?$_GET['lang']:$lang;
        $assigns['bindingsource']="/admincp/backend/system/lang_binding/$lang";
        echo $c=$this->load->view('backend/05_setting/03_gridlang',$assigns,true);
    }
    function lang_binding($lang='en') {
        $this->load->model('backend/lang_model');
        $result = $this->lang_model->binding($lang);
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    function setting(){
        $assigns['menuactive']='menu-system';
        $assigns['conf_list'] = $this->setting_model->gets();
        $colModel = $this->setting_model->colmod();
        foreach ($colModel as $col) {
            $datafields[] = array(
                'name' => $col['datafield'],
                'type' => isset($col['type']) ? $col['type'] : 'string'
            );
            $column = $col;
            $column['editable'] = isset($col['editable']) ? $col['editable'] : false;
            $column['filterable'] = isset($col['filterable']) ? $col['filterable'] : true;
            $column['sortable'] = isset($col['sortable']) ? $col['sortable'] : true;
            $column['minwidth'] = isset($col['minwidth']) ? $col['minwidth'] : (isset($col['width']) ? $col['width'] : 100);
            $columns[$col['datafield']] = $column;
            $list[] = array(
                'label' => $col['text'],
                'value' => $col['datafield'],
                'checked' => isset($col["hidden"]) ? !$col["hidden"] : true
            );
        }
        $assigns['datafields']= $datafields;
        $assigns['columns']= $columns;
        $assigns['list']= $list;
        $assigns['bindingsource']='/backend/system/binding';
        $this->smarty
                ->assign($assigns)
                ->display("backend/05_setting/01_list");
    }
    function config_editor($id = "") {
        $assigns = null;
        if ($id !== "")
            $assigns['config'] = $this->setting_model->get($id);
        $this->smarty
                ->assign($assigns)
                ->display("backend/05_setting/02_editor");
    }
    function lang_editor($id = "") {
        $assigns = null;
        if ($id !== ""){
            $this->load->model('backend/lang_model');
            $assigns['item'] = $this->lang_model->getbykey($id);
        }
        $this->smarty
                ->assign($assigns)
                ->display("backend/05_setting/04_langeditor");
    }
    function lang_save(){
        $R["result"] = -1;$rs=false;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if($this->privilege->aupr_permission!=777)goto result;
        $params=$this->input->post();
        $key=$params['lang_key'];
        $en=$params['lang_en'];
        $vi=$params['lang_vi'];
        if(!empty($key)){
            $this->load->model('backend/lang_model');
            if(!empty($en)){
                $ai=array(
                    'lang_key'=>$key,'lang_language'=>'en','lang_text'=>$en
                );
                $this->lang_model->insert_on_duplicate($ai,array('lang_text'));
            }
            if(!empty($vi)){
                $ai=array(
                    'lang_key'=>$key,'lang_language'=>'vi','lang_text'=>$vi
                );
                $this->lang_model->insert_on_duplicate($ai,array('lang_text'));
            }
        }else{
            $R["result"] = -1;
            $R["message"] = ascii_to_entities("Data is invalid.");
        }
        
        $R["result"] = 1;
        $R["message"] = ascii_to_entities("Dữ liệu đã được hệ thống ghi nhận.");
        
        result:
        echo json_encode($R);
    }
    function config_save() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if($this->privilege->aupr_permission!=777)goto result;
        $params=$this->input->post();
        $id = (int)$params["conf_id"];unset($params["conf_id"]);
        if ($id == 0)unset($params["conf_id"]);
        $updates=array('conf_name','conf_value','conf_desc');
        $rs = @$this->setting_model->insert_onduplicate_update_apri($params,$updates);
        if ($rs === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Dữ liệu đã được hệ thống ghi nhận.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    Dữ liệu chưa được hệ thống ghi nhận.<br/>
                    Vui lòng kiểm tra dữ liệu và thử lại.<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        result:
        echo json_encode($R);
    }
    function dblog(){
        $this->smarty
                ->display("backend/06_dblog/01_dblog");
    }
    function reloadpermission(){
        if(!isset($_SESSION['auth'])) echo '<script type="text/javascript">alert("Fail")</script>';
        $this->load->model("backend/auth_model");
        $tmp = @$this->auth_model->getprivileges($_SESSION['auth']['user']->ause_id);
        $privileges = array();
        if ($tmp !== null)
            foreach ($tmp as $pve) {
                $privileges[$pve->apri_key] = $pve;
            }
        $_SESSION["auth"]["privileges"] = $privileges;
        @$this->auth_backend->fileverify();
        echo '<script type="text/javascript">alert("Success")</script>';
    }
    function binding($type=null) {
        $result = $this->setting_model->binding($type);
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    function clearcache() {
        $assigns['menuactive']='menu-system';
        $assigns['caches']=array(
            'home'=>''
        );
        $this->smarty
                ->assign($assigns)
                ->display("backend/other/01_clearcache");
    }
}
?>
